package com.etc.dao.impl;

import java.util.List;

import com.etc.dao.ItemsDao;
import com.etc.dto.ItemsDto;
import com.etc.entity.Items;
import com.etc.util.DBUtil;
import com.etc.util.PageData;

/**
 * ItemsDaoImpl
 * @author Administrator
 *
 */
public class ItemsDaoImpl implements ItemsDao{
	//新增订单项 
	@Override
	public boolean addItems(Items items) {
		String sql="insert into tbl_items values(null,?,?,?,?)";
		return DBUtil.exUpdate(sql, items.getCount(),items.getGoodsid(),items.getOrderid(),items.getCost())>0;
	}
	//删除订单项
	@Override
	public boolean delItems(int itemsid) {
		String sql="delete from tbl_items where itemsid=?";
		return DBUtil.exUpdate(sql, itemsid)>0;
	}
	//修改订单项中的数量和花费
	@Override
	public boolean UpItems(int count,double cost,int itemsid) {
		String sql="update tbl_items set count=? ,cost=? where itemsid=?";
		return DBUtil.exUpdate(sql, count,cost,itemsid)>0;
	}
	//查看商店中的所有订单项
	@Override
	public PageData <Items> queryItemsShop(int page, int pageSize, int shopid) {
		String sql="select * from tbl_items  where goodsid in (select goodsid from tbl_goods  where shopid=1)";
		return DBUtil.exQueryByPage(sql, Items.class, page, pageSize, shopid);
	}
	//商店中单一商品的订单项
	@Override
	public PageData <Items> queryItemsGoods(int page, int pageSize, int goodsid) {
		String sql="select * from tbl_items  where goodsid=?";
		return DBUtil.exQueryByPage(sql, Items.class, page, pageSize, goodsid);
	}
	//查看订单(一个订单下的多个订单项)
	@Override
	public PageData<ItemsDto> queryItemsOrder(int page, int pageSize, String orderid) {
		//String sql="select * from tbl_items  where orderid=?";
		
		String sql = "SELECT tbl_items.itemsid,tbl_items.count,tbl_items.goodsid,tbl_items.userid,tbl_items.shopid,tbl_items.orderid,tbl_items.cost,tbl_goods.goodsname,tbl_shop.shopname,tbl_user.username FROM tbl_items INNER JOIN tbl_goods ON tbl_items.goodsid=tbl_goods.goodsid INNER JOIN tbl_shop ON tbl_items.shopid=tbl_shop.shopid INNER JOIN tbl_user ON tbl_items.userid=tbl_user.userid WHERE tbl_items.orderid=?";
		
		return DBUtil.exQueryByPage(sql, ItemsDto.class, page, pageSize, orderid);
	}
	//查看订单项详情
	@Override
	public Items queryOneItems(int itemsid) {
		String sql="select * from tbl_items  where itemsid=?";
				
		List<Items > list =  (List<Items>) DBUtil.exQuery(sql, Items.class, itemsid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
		
	}

}
